// gjw/pages/jishi/youxuan_info.js

const app=getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_menu: ['宝贝详情', '宝贝评价'],
    activeIndex: 0,
    index: 0,
    contxt: [
      { avatar: 'https://img01.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-e7e784240dab719c6e65117bcc3abd7a.jpg', username: '林志玲', time: '08-01', content: '这里是评论内容这里是评论内容这里是评论内容' },
      { avatar: 'https://img01.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-e7e784240dab719c6e65117bcc3abd7a.jpg', username: '林志玲', time: '08-01', content: '这里是评论内容这里是评论内容这里是评论内容' },
    ],

    noData: false,
    pl_list: true,
    box: true,



    imgUrls: ['http://www.xgyataigj.net/uploads/180522/4-1P5221A55A24.jpg', 'http://www.xgyataigj.net/uploads/180522/4-1P5221A524234.jpg', 'http://www.xgyataigj.net/uploads/180522/4-1P5221A414923.jpg'],
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 500,
    circular: true,

    out_list: [
      { img: 'https://img04.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-bec953997d224b9a1fae638a7fbf2cce.jpg', user: '小明', price: 1988, time: '18-08-06 12:32:59' }
    ],
    tan: true,
    zan: '../images/zan.png',
    zanTxt: '我要点赞',
    css: 'goods_zan',
    rule: [
      { ico: '../images/paimai.png', txt: '限时砍价  价高者得' },
      { ico: '../images/bao.png', txt: '担保交易  为您的出价保价护航' },
      { ico: '../images/zhen.png', txt: '买卖随心' },
    ],
    gz: 0,
    box:true,
    tag: '',
    star: [],
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    options.img = options.img.split(',')
    options.time = options.time.slice(0, 11)
    app.location_attachurl(this)
    app.location_url(this)
		var siteUrl = wx.getStorageSync('siteUrl')
    that.setData({
      info: options,
      siteUrl: siteUrl,
      s_agree: options.agree,
      view: options.view
    })
    that.getData()
    that.countdown()
    that.dz()
    that.gz()
    that.view()
    that.get_pl()
  },


  // 获取评论
  get_pl: function (e) {
    var that = this
    var show = false
    var hide = true
    var g_id = that.data.info.id
    app.util.request({
      url: 'entry/wxapp/Storeassessget',
      data: { store_id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            noData: hide,
            pl_list: show,
            contxt: res.data.res
          })
        } else if (res.data.code == 500) {
          that.setData({
            pl_list: hide,
            noData: show
          })
        }
      }
    })
  },
 

  tabClick: function (e) {
    
    this.setData({
      sliderOffset: e.currentTarget.dataset.offsetLeft,
      activeIndex: e.currentTarget.dataset.index
    })
  },

  blur: function (e) {
    var hide = true
    this.setData({
      box: hide
    })
  }, 


  pai: function (e) {
    var that = this
    var show = false
    var hide = true
    that.setData({
      tan: show
    })
  },

  // 判断是否关注
  gz: function (e) {
    var that = this
    var gz = that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.info.phone
    
    
    if (gz == 0) {
      app.util.request({
        url: 'entry/wxapp/Islovestore',
        data: {
          openid: openid, phone: phone
        },
        success: function (res) {
          
          if (res.data.code == 500) {
            that.setData({
              gz: 0
            })
          } else if (res.data.code == 200)
            that.setData({
              gz: 1
            })
        }
      })
    }
  },

  // 判断是否已经点赞
  dz: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    
    
    app.util.request({
      url: 'entry/wxapp/Storeagreedetail',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            zan: '../images/zan_on.png',
            zanTxt: '已点赞',
            css: 'on_zan',
          })
        }
      }
    })
  },

  //规则
  rule: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/marketrule',
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            rule: res.data.res
          })
        }
      }
    })
  },

 

  // 点赞
  clickZan: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    
    
    app.util.request({
      url: 'entry/wxapp/Storeagree',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          wx.showToast({
            title: '点赞成功',
          })
          that.setData({
            zan: '../images/zan_on.png',
            zanTxt: '已点赞',
            css: 'on_zan',
            s_agree: res.data.res
          })
        } else if (res.data.code == 201) {
          wx.showToast({
            title: '您已经点过赞了',
            icon: 'none',
          })
        }
      }
    })


  },

  // 浏览量
  view: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    app.util.request({
      url: 'entry/wxapp/Storeview',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            view: res.data.res
          })
        }
      }
    })
  },


  // 时间
  countdown: function (time) {
    var that = this
    var EndTime = that.data.info.totimesjc
    var NowTime = Math.round(new Date().getTime() / 1000)
    var total_micro_second = EndTime - NowTime
    // console.log(EndTime)
    // console.log(NowTime)
    // console.log(total_micro_second)
    if (total_micro_second <= 0) {   //倒计时结束
      // app.util.request({
      //   url: 'entry/wxapp/UpdateGroup',
      //   data: {
      //     store_id: that.data.id
      //   },
      //   success: res => {
      //     
      //   }
      // })
      // that.setData({
      //   clock: false
      // });

      that.setData({
       jitime:0
      })
    } else if (total_micro_second > 0) {
      that.dateformat(total_micro_second)
      setTimeout(function () {
        total_micro_second -= 1000;
        that.countdown(time);
      }, 1000)
    }

  },

  dateformat: function (e) {
    var that = this
    // 总秒数
    var second = Math.floor(e);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    if (day < 10) {
      day = '0' + day
    }
    if (hr < 10) {
      hr = '0' + hr
    }
    if (sec < 10) {
      sec = '0' + sec
    }
    if (min < 10) {
      min = '0' + min
    }
    that.setData({
      day: day,
      hour: hr,
      min: min,
      sec: sec
    })
  },

  // 关注
  follow: function (e) {
    var that = this
    var gz = that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.info.phone
    
    
    
    if (gz == 0) {
      app.util.request({
        url: 'entry/wxapp/Lovestore',
        data: {
          openid: openid, phone: phone
        },
        success: function (res) {
          
          that.setData({
            gz: 1
          })
          wx.showToast({
            title: '关注成功',
            icon: 'success'
          })
        }
      })
    } else {
      wx.showToast({
        title: '您已关注过该店铺了',
        icon: 'none'
      })
    }
  },

  change:function(e){
    
    this.setData({
      cash:e.detail.value
    })
  },

 


  // 查看大图
  previewImage: function (e) {
    
    var that = this
    var url = that.data.siteUrl   // {{siteUrl}}attachment/wff/    地址前缀
    var urls = []
    var current
    var index = e.currentTarget.dataset.index   //当前点击的下标
    var pictures = that.data.info.img

    for (let i in pictures) {
      urls.push(url + 'attachment/' + pictures[i])
    }
    wx.previewImage({
      current: url + 'attachment/' + pictures[index],
      urls: urls
    })
  },

  // 店铺评分
  getData: function (e) {
    var that = this
    var phone = that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Lovestoreinfo',
      data: {
        phone: phone
      },
      success: function (res) {
        
        that.setData({
          tag: res.data
        })
        var num = that.data.tag.star
        var star = []
        for (var i = 0; i < num; i++) {
          star.push('../images/star_on.png')
        }
        that.setData({
          star: star
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {
		this.videoContext = wx.createVideoContext('myVideo')
	},
	play: function (e) {
		this.videoContext.requestFullScreen()
	},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})